﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MauiApp1
{
    public class TableColumn : ContentView
    {
        public static readonly BindableProperty FieldNameProperty = BindableProperty.Create(nameof(FieldName), typeof(string), typeof(TableColumn), null);
        public string FieldName
        {
            get => (string)GetValue(FieldNameProperty);
            set => SetValue(FieldNameProperty, value);
        }

        public static readonly BindableProperty ColumnWidthProperty = BindableProperty.Create(nameof(ColumnWidth), typeof(GridLength), typeof(TableColumn), GridLength.Star, BindingMode.OneWay, null);

        [TypeConverter(typeof(GridLengthTypeConverter))]
        public GridLength ColumnWidth
        {
            get => (GridLength)GetValue(ColumnWidthProperty);
            set => SetValue(ColumnWidthProperty, value);
        }

        public static readonly BindableProperty ColumnNameProperty = BindableProperty.Create(nameof(ColumnName), typeof(string), typeof(TableColumn), null);


        public string ColumnName
        {
            get => (string)GetValue(ColumnNameProperty);
            set => SetValue(ColumnNameProperty, value);
        }

        public static readonly BindableProperty HeaderDataTemplateProperty = BindableProperty.Create(nameof(HeaderDataTemplate), typeof(DataTemplate), typeof(TableColumn), null);

        public DataTemplate HeaderDataTemplate
        {
            get => (DataTemplate)GetValue(HeaderDataTemplateProperty);
            set => SetValue(HeaderDataTemplateProperty, value);
        }

        public View? LoadDataTemplateContent(object bindingContext)
        {
            if (HeaderDataTemplate != null && bindingContext != null)
            {
                // 使用 DataTemplate 生成内容  
                var content = HeaderDataTemplate.CreateContent();
                // 获取生成的视图  
                if (content is View view)
                {
                    view.BindingContext = bindingContext; // 绑定上下文  
                    return view;
                }
            }
            return null;
        }
    }
}
